//
//  ZGBaseViewController.m
//  NewBee
//
//  Created by leergou on 16/8/20.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "ZGBaseViewController.h"
#import "ZGNavButton.h"
#import "ZGNavTitleView.h"
#import "ZGQRCodeController.h"
#import "ZGSearchController.h"
#import "ZGSendAddressController.h"

#define kMargin 5

@interface ZGBaseViewController ()

@property (nonatomic, weak) UIButton *leftButton;

@property (nonatomic, weak) UIButton *rightButton;

@property (nonatomic, weak) UIView *titleView;

@end

@implementation ZGBaseViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupNav];
    
    
}

#pragma mark - 按钮点击事件 -
- (IBAction)leftItemClick:(UIButton *)sender{
    NSLog(@"左侧按钮点击");
    // 加载二维码扫描页面
    ZGQRCodeController *codeVc = [[ZGQRCodeController alloc] init];
    
    [self.navigationController pushViewController:codeVc animated:YES];
    
}

- (IBAction)rightItemClick:(UIButton *)sender{
    NSLog(@"右侧按钮点击");
    // 加载搜索页面
    ZGSearchController *codeVc = [[ZGSearchController alloc] init];
    
    [self.navigationController pushViewController:codeVc animated:YES];
    
}

- (void)tapGes:(UIGestureRecognizer *)tap{
    NSLog(@"点击配送");
    ZGSendAddressController *addressVc = [[ZGSendAddressController alloc] init];
    addressVc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:addressVc animated:YES];
    
}

#pragma mark - 设置UI -
- (void)setupUI{
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 设置导航栏 -
- (void)setupNav{
    
#warning 封装后不显示了???
    // 左侧按钮
    ZGNavButton *leftButton = [ZGNavButton buttonWithType:UIButtonTypeCustom];
    self.leftButton = leftButton;
    
    [leftButton setImage:[UIImage imageNamed:@"icon_black_scancode"] forState:UIControlStateNormal];
    
    [leftButton setTitle:@"扫一扫" forState:UIControlStateNormal];
    
    [leftButton addTarget:self action:@selector(leftItemClick:) forControlEvents:UIControlEventTouchUpInside];

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    // 中间按钮
    ZGNavTitleView *titleView = [[ZGNavTitleView alloc] init];
    self.titleView = titleView;
    self.navigationItem.titleView = titleView;
    
    // 添加手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
    
    [self.titleView addGestureRecognizer:tapGes];
    
    // 右侧按钮
    ZGNavButton *rightButton = [ZGNavButton buttonWithType:UIButtonTypeCustom];
    
    self.rightButton = rightButton;
    
    [rightButton setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    
    [rightButton setTitle:@"搜索" forState:UIControlStateNormal];
    
    [rightButton addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}


#pragma mark - 布局子控件 -
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.leftButton.frame = CGRectMake(5, 0, 30, 44);
    
    self.rightButton.frame = CGRectMake(self.view.frame.size.width - 2 * kMargin - self.leftButton.frame.size.width, 0, 30, 44);
    
    CGFloat titleViewWidth = 120;
    self.titleView.frame = CGRectMake((SCREEN_WIDTH - titleViewWidth) * 0.5, 0, titleViewWidth, 44);
}

#pragma mark - 自定义方法 -
#pragma mark - 创建按钮
- (ZGNavButton *)addButtonWithTitle:(NSString *)title image:(NSString *)imageName target:(id)target action:(SEL)action{
    
    ZGNavButton *button = [ZGNavButton buttonWithType:UIButtonTypeCustom];
    
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button addTarget:target action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}




@end